﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using mshtml;

namespace HTMLEditorProjekt
{
    public partial class HTMLEditorControl : UserControl
    {
        private IHTMLDocument2 doc;

        private string documentText = "";
        public string DocumentText
        {
            get { return getDocumentHTMLText(); }
            set { documentText = value; }
        }


        public HTMLEditorControl()
        {
            InitializeComponent();
        }

        private void HTMLEditorControl_Load(object sender, EventArgs e)
        {
            HTMLEditor.DocumentText = "";
            HTMLEditor.DocumentText = documentText;
            //HTMLEditor.DocumentText = Resource1.bspText.Replace("\n", "<br/>");

            doc = HTMLEditor.Document.DomDocument as IHTMLDocument2;
            doc.designMode = "On";
        }

        private void btnBold_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("Bold", false, null);
        }

        private void btnUnderline_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("Underline", false, null);
        }

        private void btnItalic_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("Italic", false, null);
        }

        private void btnFrontColor_Click(object sender, EventArgs e)
        {
            DialogResult res = colorDialog1.ShowDialog();
            if (res == DialogResult.OK)
            {
                HTMLEditor.Document.ExecCommand("ForeColor", false, getRGBCode(colorDialog1.Color));
            }

        }



        private string getRGBCode(Color Farbe)
        {
            string result = "#";
            string tmp;

            tmp = Farbe.R.ToString("x").ToUpper();
            if (tmp.Length < 2) tmp = "0" + tmp;
            result += tmp;

            tmp = Farbe.G.ToString("x").ToUpper();
            if (tmp.Length < 2) tmp = "0" + tmp;
            result += tmp;

            tmp = Farbe.B.ToString("x").ToUpper();
            if (tmp.Length < 2) tmp = "0" + tmp;
            result += tmp;

            return result;
        }

        private void cmbFont_Click(object sender, EventArgs e)
        {



        }

        private void cmbFont_TextChanged(object sender, EventArgs e)
        {

        }

        private void cmbFont_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbFont.SelectedItem != null)
                {
                    HTMLEditor.Document.ExecCommand("FontName", false, cmbFont.SelectedItem.ToString());
                    cmbFont.SelectedItem = null;
                }

            }
            catch
            {


            }
        }

        private void cmbFontSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbFontSize.SelectedItem != null)
                {
                    HTMLEditor.Document.ExecCommand("FontSize", false, cmbFontSize.SelectedItem.ToString());
                    cmbFontSize.SelectedItem = null;
                }
            }
            catch
            {


            }
        }

        private void btnBackColor_Click(object sender, EventArgs e)
        {
            DialogResult res = colorDialog1.ShowDialog();
            if (res == DialogResult.OK)
            {
                HTMLEditor.Document.ExecCommand("BackColor", false, getRGBCode(colorDialog1.Color));
            }
        }

        private void btnListe_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("InsertUnorderedList", false, null);
        }

        private void btnOrderedList_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("InsertOrderedList", false, null);

        }

        private void btnTextLinks_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("JustifyLeft", false, null);

        }

        private void btnTextCenter_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("JustifyCenter", false, null);

        }

        private void btnTextRight_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("JustifyRight", false, null);

        }

        private void btnTextBlock_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("JustifyFull", false, null);

        }

        private void btnEinrücken_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("Indent", false, null);

        }

        private void btnRückrücken_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("Outdent", false, null);
        }

        private void btnHoch_Click(object sender, EventArgs e)
        {

            HTMLEditor.Document.ExecCommand("Subscript", false, null);
        }

        private void btnRunter_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("Superscript", false, null);
        }

        private void btnLink_Click(object sender, EventArgs e)
        {
            IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;


            InsertLink frm = new InsertLink(range.text);
            frm.ShowDialog();

            range.pasteHTML(frm.URL);
            range.collapse(false);
            range.select();
        }

        private void btnUnlink_Click(object sender, EventArgs e)
        {
            HTMLEditor.Document.ExecCommand("Unlink", false, null);
        }



        private void btnQuelltext_Click_1(object sender, EventArgs e)
        {
            if (HTMLEditor.Visible)
            {
                HTMLEditorQuellCode.Text = HTMLEditor.DocumentText;
                HTMLEditor.Visible = false;
                HTMLEditorQuellCode.Visible = true;
                toolStrip1.Enabled = false;
                toolStrip2.Enabled = false;
            }
            else
            {

                //HTMLEditor.DocumentText = HTMLEditorQuellCode.Text;
                //HTMLEditor.DocumentText = HTMLEditorQuellCode.Text;
                HTMLEditor.Visible = true;
                HTMLEditorQuellCode.Visible = false;
                toolStrip1.Enabled = true;
                toolStrip2.Enabled = true;
            }
        }

        public string getDocumentHTMLText()
        {
            string tmp = HTMLEditor.DocumentText;

            tmp = tmp.Substring(tmp.IndexOf("<BODY>") + 6);
            tmp = tmp.Remove(tmp.IndexOf("</BODY"));
            return tmp;
        }



    }
}
